package dao.impl;

import dao.ImgDao;
import domain.Img;
import org.apache.commons.dbutils.QueryRunner;
import util.DataSourceUtils;
import util.JDBCUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ImgDaoImpl implements ImgDao {

    @Override
    public List<Img> findAllImg() throws SQLException {
        String sql ="select * from img";
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        return runner.query(sql, rs -> {
            List<Img> imgList = new ArrayList<>();
            // 循环遍历订单项和商品信息
            Img img;
            while ((img = JDBCUtil.parseResultSet(rs, Img.class)) != null) {
                imgList.add(img);
            }
            return imgList;
        });
    }

    @Override
    public int findImgById(int id, String url) throws SQLException {
//        String sql ="select * from img where id = ?";
        String sql = "update img set url = ? where id = ?";

        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        return runner.update(sql,  url, id);
    }
}
